/*
 * Copyright (c) 2020 - present, Inspur Genersoft Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.inspur.edp.cef.generator.common;


import com.inspur.edp.cef.designtime.api.element.GspElementObjectType;
import com.inspur.edp.lcm.metadata.api.entity.MetadataProject;
import com.inspur.edp.lcm.metadata.api.service.MetadataProjectService;
import com.inspur.edp.lcm.metadata.api.service.MetadataService;
import com.inspur.edp.udt.designtime.api.entity.SimpleDataTypeDef;
import com.inspur.edp.udt.designtime.api.entity.UnifiedDataTypeDef;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.util.Objects;

//TODO:这个类将来可能放到Cef里
public final class MetadataUtil
{
	@Autowired
	private static MetadataProjectService getMetadataProjService;
	@Autowired
 private static MetadataService getMetadataService;

// TODO: java版临时注释 应该用不到了
//	 static FsService getFsService()
//	/**
//	 根据元数据相对路径获取所属元数据工程输出路径
//
//	*/
//
//	public static String  getBelongProjectOutputDir(String metadataPath)
//	{
//		return Path.Combine(getMetadataService().getRefPath(metadataPath), getProjectTargetFramework(getProjectPathByMetadataPath(metadataPath)));
//	}

	public static String  getProjectPathByMetadataPath(String metadataRelativePath) throws Exception {
		//DataValidator.CheckForEmptyString(metadataRelativePath, "metadataRelativePath");

		MetadataProject projectInfo = getMetadataProjService.getMetadataProjInfo(metadataRelativePath);
		return new File(new File(projectInfo.getProjectPath()), projectInfo.getProjectFileName()).getPath();
	}

	//TODO: java版临时注释 应该用不到了
//	private static String  getProjectTargetFramework(String projectPath)
//	{
//		if (DotNetToJavaStringHelper.isNullOrEmpty(projectPath) || !getFsService().ExistsAsFile(projectPath))
//		{
//			throw new GSPException("CafGen_3001", FormatExceptionMsg("元数据工程不存在, 请先创建元数据工程再创建元数据"));
//		}
//		XmlDocument doc = new XmlDocument();


////		using (var projStream = FsService.Read(projectPath))
//		var projStream = getFsService().Read(projectPath);
//		try
//		{
//			doc.Load(projStream);
//		}
//		finally
//		{
//			projStream.dispose();
//		}
//

//		var framework = doc.SelectSingleNode("/Project/PropertyGroup/TargetFramework");
//		if (framework == null)
//		{
//			throw new GSPException("CafGen_3002", FormatExceptionMsg("无效的元数据工程"));
//		}
//
//		return framework.InnerText;
//	}

	public static String  formatExceptionMsg(String msg, String... pars)
	{
		if (pars != null && pars.length > 0)
		{
			msg = String.format(msg, pars);
		}
		final String msgFlag = "#GSPBefError#";
		return msgFlag + msg + msgFlag;
	}

	public static boolean isEnrichedAssoUdt(UnifiedDataTypeDef udtDef){
		Objects.requireNonNull(udtDef);

		SimpleDataTypeDef sdtDef = (SimpleDataTypeDef) udtDef;
		return sdtDef != null && sdtDef.getObjectType() == GspElementObjectType.Association && sdtDef
				.getContainElements().get(0).containRefElementNotFromAssoUdt();
	}
}
